﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;


public partial class App_Input_DishUpdForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.Context.Items["DISH_ID"] != null)
            {
                this.ViewState["DISH_ID"] = this.Context.Items["DISH_ID"];
                this.ViewState["STATUS"] = "UPD";
            }
            else
            {
                this.ViewState["DISH_ID"] = null;
                this.ViewState["STATUS"] = "";
            }
            initDic();
            if (Context.Items.Contains("ParamPageIndex"))
            {
                this.ViewState["ParamPageIndex"] = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            if (Context.Items.Contains("CMB_TIME"))
            {
                this.ViewState["TIME"] = Convert.ToString(Context.Items["CMB_TIME"]);
            }
            if (Context.Items.Contains("CMB_CLASS"))
            {
                this.ViewState["CLASS"] = Convert.ToString(Context.Items["CMB_CLASS"]);
            }
            if (Context.Items.Contains("CMB_TYPE"))
            {
                this.ViewState["TYPE"] = Convert.ToString(Context.Items["CMB_TYPE"]);
            }
            if (Context.Items.Contains("CMB_KIND"))
            {
                this.ViewState["KIND"] = Convert.ToString(Context.Items["CMB_KIND"]);
            }
            if (Context.Items.Contains("CMB_DELIVERY"))
            {
                this.ViewState["DELIVERY"] = Convert.ToString(Context.Items["CMB_DELIVERY"]);
            }
            if (Context.Items.Contains("TXT_NAME"))
            {
                this.ViewState["NAME"] = Convert.ToString(Context.Items["TXT_NAME"]);
            }

        }

    }
    void Page_PreRender(object sender, EventArgs e)
    {
        if (Convert.ToString(this.ViewState["STATUS"]) == "UPD")
        {
            loadDate();
        }
    }

    /*
     * 函数名：initPageControls
     * 功能：控件数据绑定
     * 参数：无
     * 说明：将数据字典中的数据绑定在指定的控件上
     */
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*送菜类别*/
        paramBean.Catalog = "M_DISH.DISH_DELIVERY";
        initAll(dao, paramBean, this.DrpDelivery);
        /*荤素类别*/
        paramBean.Catalog = "M_DISH.DISH_CLASS";
        initAll(dao, paramBean, this.DrpClass);
        /*菜式类别*/
        paramBean.Catalog = "M_DISH.DISH_TYPE";
        initAll(dao, paramBean, this.DrpType);
        /*用餐时间*/
        paramBean.Catalog = "M_DISH.DISH_TIME";
        initAll(dao, paramBean, this.DrpTime);
        /*用餐时间*/
        paramBean.Catalog = "M_DISH.DISH_KIND";
        initAll(dao, paramBean, this.DrpKind);
        /*食品结构*/
        paramBean.Catalog = "M_MEMBER_FOOD.FOOD_TYPE";
        initAll(dao, paramBean, this.ChkFoodType);
    }
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }

    /*
     * 函数名：loadDate
     * 功能：读取数据
     * 参数：无
     * 说明：
     */
    private void loadDate()
    {
        DishBean dataBean = new DishBean();
        DishFoodBean dataFoodBean = new DishFoodBean();
        DishDao Dao = new DishDao();

        //菜肴信息的读取
        dataBean.DishId = Convert.ToInt32(ViewState["DISH_ID"]);
        dataBean = Dao.GetDish(dataBean);
        if (dataBean != null)
        {
            this.DrpClass.SelectedValue = dataBean.DishClass;
            this.DrpDelivery.SelectedValue = dataBean.DishDelivery;
            this.DrpKind.SelectedValue = dataBean.DishKind;
            this.DrpTime.SelectedValue = dataBean.DishTime;
            this.DrpType.SelectedValue = dataBean.DishType;
            this.txtMemo.Text = dataBean.DishMemo;
            this.txtDescription.Text = dataBean.DishDescription;
            txtName.Text = dataBean.DishName;
            txtPrice.Text = Convert.ToString(Convert.ToDouble(dataBean.DishPrice));
            this.Image1.ImageUrl = "~/App/Input/DishPhotoForm.aspx?ID=" + dataBean.DishId;
        }

        //食品结构的读取
        dataFoodBean.DishId = Convert.ToInt32(ViewState["DISH_ID"]);
        IList dataList = Dao.ListDishFood(dataFoodBean);
        if (dataList.Count > 0 && this.ChkFoodType.Items.Count > 0)
        {
            for (int i = 0; i < this.ChkFoodType.Items.Count; i++)
            {
                for (int j = 0; j < dataList.Count; j++)
                {
                    DishFoodBean fdBean = (DishFoodBean)dataList[j];
                    if (this.ChkFoodType.Items[i].Value == fdBean.DishFood)
                    {
                        this.ChkFoodType.Items[i].Selected = true;
                        break;
                    }
                }
            }
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }
        SaveDate();
        returnSearch();
    }


    /*
     * 函数名：SaveDate
     * 功能：保存数据
     * 参数：无
     * 说明：
     */
    protected void SaveDate()
    {
        DishBean dataBean = new DishBean();
        DishDao Dao = new DishDao();

        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;

        dataBean.DishName = txtName.Text;
        dataBean.DishClass = DrpClass.SelectedValue;
        dataBean.DishDelivery = DrpDelivery.SelectedValue;
        dataBean.DishKind = DrpDelivery.SelectedValue;
        dataBean.DishTime = DrpTime.SelectedValue;
        dataBean.DishType = DrpType.SelectedValue;
        dataBean.DishPrice = Convert.ToDecimal(txtPrice.Text);
        dataBean.DishMemo = txtMemo.Text;
        dataBean.DishDescription = txtDescription.Text;
        dataBean.UserId = userId;
        if (this.FileUpload1.FileBytes.LongLength > 0)
        {
            dataBean.Photo = FileUpload1.FileBytes;
        }

        if (Convert.ToString(this.ViewState["STATUS"]) == "UPD")  //数据更新
        {
            dataBean.DishId = Convert.ToInt32(this.ViewState["DISH_ID"]);
            IList lst = new ArrayList();
            for (int i = 0; i < ChkFoodType.Items.Count;i++ )
            {
                if (ChkFoodType.Items[i].Selected == true)
                {
                    DishFoodBean fdBean = new DishFoodBean();
                    fdBean.DishId = Convert.ToInt32(this.ViewState["DISH_ID"]);
                    fdBean.DishFood = ChkFoodType.Items[i].Value;
                    fdBean.UserId = userId;
                    lst.Add(fdBean);
                }
            }
            if(Dao.UpdateDishLst(lst,dataBean,Convert.ToInt32(this.ViewState["DISH_ID"]))!=0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }
        }
        else //数据新规
        {
            int iDishId = (int)Dao.InsertDish(dataBean);
            dataBean.DishId = iDishId;
            if (dataBean.Photo != null)
            {
                Dao.UpdPhoto(dataBean);
            }
            IList lst = new ArrayList();
            for (int i = 0; i < ChkFoodType.Items.Count; i++)
            {
                if (ChkFoodType.Items[i].Selected == true)
                {
                    DishFoodBean fdBean = new DishFoodBean();
                    fdBean.DishId = iDishId;
                    fdBean.DishFood = ChkFoodType.Items[i].Value;
                    fdBean.UserId = userId;
                    lst.Add(fdBean);
                }
            }
            if (Dao.InsertDishFoodLst(lst, iDishId) != 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
        }
        
    }

    protected void postBack_Click(object sender, EventArgs e)
    {
        returnSearch();
    }

    /*
     * 函数名：returnSearch
     * 功能：返回信息
     * 参数：无
     * 说明：
     */
    protected void returnSearch()
    {
        string url = "~/App/Input/DishSrhForm.aspx";

        // 传递分页编号
        Context.Items["CMB_TIME"] = Convert.ToString(this.ViewState["TIME"]);
        Context.Items["CMB_CLASS"] = Convert.ToString(this.ViewState["CLASS"]);
        Context.Items["CMB_TYPE"] = Convert.ToString(this.ViewState["TYPE"]);
        Context.Items["CMB_KIND"] = Convert.ToString(this.ViewState["KIND"]);
        Context.Items["CMB_DELIVERY"] = Convert.ToString(this.ViewState["DELIVERY"]);
        Context.Items["TXT_NAME"] = Convert.ToString(this.ViewState["NAME"]);
        Context.Items["ParamPageIndex"] = Convert.ToString(this.ViewState["ParamPageIndex"]);
        ControllerUtils.Transfer(Context, url, true); 
    }

}
